﻿using System.Windows;
using System.Windows.Media;
using WPFSchematics.Definition.Entities;
using WPFSchematics.Definition.Models;

namespace WPFSchematics.Definition.Shapes
{
    /// <summary>
    /// 状态绘图。
    /// </summary>
    public class StateDrawing:Entity
    {
        /// <summary>
        /// 绘图。
        /// </summary>
        public static readonly DependencyProperty DrawingProperty = DependencyProperty.Register(
        "Drawing",
        typeof(Drawing),
        typeof(StateDrawing),
        new PropertyMetadata(OnDrawingChanged)
        );

        /// <summary>
        /// 绘图变更处理。
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void OnDrawingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(((StateDrawing)d).Drawing!=null)
            {
                ((StateDrawing)d).Drawing.Freeze();
                var b = new DrawingBrush(((StateDrawing)d).Drawing);
                
                //RenderOptions.SetCacheInvalidationThresholdMinimum(b, 0.5);
                //RenderOptions.SetCacheInvalidationThresholdMaximum(b, 2.0);
                //RenderOptions.SetCachingHint(b, CachingHint.Cache);
                
                b.Freeze();
                ((StateDrawing) d).Fill = b;
            }
        }

        /// <summary>
        /// 绘图。
        /// </summary>
        public Drawing Drawing
        {
            get { return (Drawing)GetValue(DrawingProperty); }
            set { SetValue(DrawingProperty, value); }
        }

        /// <summary>
        /// 填充。
        /// </summary>
        public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
        "Fill",
        typeof(Brush),
        typeof(StateDrawing)
        );

        /// <summary>
        /// 填充。
        /// </summary>
        public Brush Fill
        {
            get { return (Brush)GetValue(FillProperty); }
            set { SetValue(FillProperty, value); }
        }

        /// <summary>
        /// 笔画。
        /// </summary>
        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
        "Stroke",
        typeof(Pen),
        typeof(StateDrawing)
        );

        /// <summary>
        /// 笔画。
        /// </summary>
        public Pen Stroke
        {
            get { return (Pen)GetValue(StrokeProperty); }
            set { SetValue(StrokeProperty, value); }
        }

        /// <summary>
        /// 状态类型。
        /// </summary>
        public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
         "StateType",
         typeof(StateType),
         typeof(StateDrawing)
         );

        /// <summary>
        /// 状态类型。
        /// </summary>
        public StateType StateType
        {
            get { return (StateType)GetValue(StateProperty); }
            set { SetValue(StateProperty, value); }
        }
    }
}
